package servlets1;

import beans1.Mzbean;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import service1.MZService;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;

import java.util.ArrayList;
@WebServlet("/Main_MZ")
public class Main_MZ extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action=request.getParameter("action");
        System.out.println("in Main.java,the action is "+action);
        if("delMZ".equals(action))
        {
            System.out.println("i am in delOffice");
            delMZ(request, response);
        }

        else if("editMZ".equals(action)) {
            System.out.println("i am in editOffice");
            try {
				editMZ(request, response);
			} catch (ParseException | IOException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            System.out.println("i am out editOffice");
        } else if ("insertMZ".equals(action)) {
            System.out.println("i am in insertOffice");
            try {
                insertMZ(request,response);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        } else
        {
            System.out.println("i am in showlist");
            showlist(request, response);
        }
    }

    private void insertMZ(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException, ParseException {
        MZService mzService=new MZService();
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String ksid = request.getParameter("ksid");
        Mzbean str=new Mzbean(id, name,ksid);
        mzService.insertMZ(str);
        response.sendRedirect(request.getContextPath()+"/Main_MZ");
    }

    private void editMZ(HttpServletRequest request, HttpServletResponse response) throws ParseException, IOException, SQLException {
        MZService mzService=new MZService();
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String ksid = request.getParameter("ksid");
        Mzbean str=new Mzbean(id,name,ksid);
        System.out.println("in the editksid="+str.getKsid());
        mzService.editMZ(str);
        response.sendRedirect(request.getContextPath()+"/Main_MZ");
    }

    private void delMZ(HttpServletRequest request, HttpServletResponse response) throws IOException {
        MZService mzService=new MZService();
        String id = request.getParameter("id");
        Mzbean str=new Mzbean();
        str.setId(id);
        int result=mzService.delMZ(str);
        if(result==1) {
            System.out.println("success");
        }
        else {
            System.out.println("false");
        }
        response.sendRedirect(request.getContextPath()+"/Main_MZ");
    }

    private void showlist(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        MZService mzService=new MZService();
        ArrayList<Mzbean> Mzbeans = mzService.browse();
        request.setAttribute("list", Mzbeans);
        request.getRequestDispatcher("admin/mzManage.jsp").forward(request,response);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
